<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>管理员界面</title>
    <link rel="shortcut icon" th:href="@{/logo/favicon.ico}"/>
    <!-- Custom fonts for this template-->
    <link rel="stylesheet" type="text/css" th:href="@{/vendor/fontawesome-free/css/all.min.css}"/>
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
          rel="stylesheet">
    <!-- Custom styles for this template-->
    <link rel="stylesheet" type="text/css" th:href="@{/css/sb-admin-2.min.css}"/>
    <!-- Custom styles for this page -->
    <link rel="stylesheet" type="text/css" th:href="@{/vendor/datatables/dataTables.bootstrap4.min.css}"/>
</head>
<body id="page-top">
<!-- Page Wrapper -->
<div id="wrapper">
    <!-- Sidebar -->
    <div th:if="${role} == 2" style="height: auto;background-color: #224abe;">
        <div th:replace="commons/admin_sidebar::#accordionSidebar"></div>
    </div>
    <div th:if="${role} == 4" style="height: auto;background-color: #224abe;">
        <div th:replace="commons/aduit_sidebar::#accordionSidebar"></div>
    </div>
    <!-- End of Sidebar -->
    <!-- Content Wrapper -->
    <div id="content-wrapper" class="d-flex flex-column">
        <!-- Main Content -->
        <div id="content">
            <!-- Topbar -->
            <div th:replace="commons/admin_navbar::#navbar"></div>
            <!-- Begin Page Content -->
            <div class="container-fluid">
                <div class="row">
                    <div class="card shadow  col-md-4">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold text-primary" style="text-align: center">添加用户</h6>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-sm-4 text-sm-right">选择角色</div>
                                <div class="col-sm-8">
                                    <select name="" id="select_role" class="form-control">
                                        <option value="1">普通用户</option>
                                        <option value="6">管理基本数据</option>
                                        <option value="2">审核员</option>
                                        <option value="3">批准员</option>
                                    </select>
                                </div>
                            </div>
                            <hr>
                            <div class="row">
                                <div class="col-sm-4 text-sm-right">持有者姓名</div>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="holder">
                                </div>
                            </div>
                            <hr>
                            <div class="row">
                                <div class="col-sm-4 text-sm-right">登录名</div>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="username">
                                </div>
                            </div>
                            <hr>
                            <div class="row">
                                <div class="col-sm-4 text-sm-right">密码</div>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="pwd">
                                </div>
                            </div>
                            <hr>
                            <div class="row">
                                <a onclick="add_user()" href="#" class="btn btn-success btn-icon-split"
                                   style="margin: 0px auto">
                                    <span class="text">提交</span>
                                </a>
                            </div>
                        </div>

                    </div>
                    <div class="card shadow  col-md-8">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold text-primary" style="text-align: center">审核报告</h6>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover text-nowrap" id="dataTable"
                                       cellspacing="0">
                                    <thead>
                                    <tr>
                                        <th>用户id</th>
                                        <th>登录名</th>
                                        <th>持有者姓名</th>
                                        <th>角色</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.container-fluid -->
        </div>
        <!-- End of Main Content -->
        <!-- Footer -->
        <div th:replace="commons/footer::#footer"></div>
        <!-- End of Footer -->

    </div>
    <!-- End of Content Wrapper -->

</div>
<!-- End of Page Wrapper -->

<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
</a>

<!-- Logout Modal-->
<div th:replace="commons/logout::#logoutModal"></div>


<!-- Bootstrap core JavaScript-->
<script type="text/javascript" th:src="@{/vendor/jquery/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/vendor/bootstrap/js/bootstrap.bundle.min.js}"></script>

<script type="text/javascript" th:src="@{/js/admin_commons.js}"></script>
<!-- Core plugin JavaScript-->
<script type="text/javascript" th:src="@{/vendor/jquery-easing/jquery.easing.min.js}"></script>
<!-- Custom scripts for all pages-->

<script type="text/javascript" th:src="@{/js/sb-admin-2.min.js}"></script>

<!-- Page level plugins -->
<script type="text/javascript" th:src="@{/vendor/datatables/jquery.dataTables.min.js}"></script>
<script type="text/javascript" th:src="@{/vendor/datatables/dataTables.bootstrap4.min.js}"></script>

<script type="text/javascript" th:src="@{/js/demo/datatables-demo.js}"></script>
</body>
<script>

    function add_user() {
        if (confirm("确认添加?")) {
            $.ajax({
                url: "/admin/add-user",
                method: "post",
                contentType: "application/json;charset=utf-8",
                data:JSON.stringify({"roleId":$("#select_role").val(),"holder":$("#holder").val(),
                    "username":$("#username").val(),"password":$("#pwd").val(),"roleName":$("#select_role").find("option:selected").text().trim()}),
                success:function (res) {
                    if(res.code == 200){
                        $("#holder").val("");
                        $("#username").val("");
                        $("#pwd").val("");
                        $("#dataTable").DataTable().ajax.reload();
                    }else{
                        alert(res.message);
                    }
                }
            });
        }
    }

    $(function () {
        var table;

        //提示信息
        $.fn.dataTable.ext.errMode = 'none';
        var lang = {
            "bAutoWidth": false,
            "sProcessing": "处理中...",
            "sLengthMenu": "每页 _MENU_ 项",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "当前显示第 _START_ 至 _END_ 项，共 _TOTAL_ 项。",
            "sInfoEmpty": "当前显示第 0 至 0 项，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页",
                "sJump": "跳转"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        };
        //初始化表格
        table = $("#dataTable")
            .on('error.dt', function (e, settings, techNote, message) {
                console.warn(message)
            }).dataTable({
                destroy: true,
                autoWidth: false,
                language: lang, //提示信息
                stripeClasses: ["odd", "even"], //为奇偶行加上样式，兼容不支持CSS伪类的场合
                processing: true, //隐藏加载提示,自行处理
                serverSide: false, //启用服务器端分页
                searching: true, //禁用原生搜索
                orderMulti: true, //启用多列排序
                order: [1, "asc"], //按照第二列进行排序
                renderer: "bootstrap", //渲染样式：Bootstrap和jquery-ui
                pagingType: "simple_numbers", //分页样式：simple,simple_numbers,full,full_numbers
                columnDefs: [{
                    "targets": 'nosort', //列的样式名
                    "orderable": false //包含上样式名‘nosort'的禁止排序
                }],
                ajax: function (data, callback, settings) {
                    $.ajax({
                        type: "GET",
                        beforeSend: function (request) {
                            request.setRequestHeader("uid", localStorage.getItem("token"));
                        },
                        url: "/admin/get-user-all",
                        // url: "/admin/view-all?status=4",
                        cache: false, //禁用缓存
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.data = result.data;//返回的数据列表
                            callback(returnData);
                        }
                    });
                },
                "columns": [
                    {"data": "userId", visible: false},
                    {"data": "username"},
                    {"data": "holder"},
                    {"data": "roleName"}
                ],
                "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {                    //列样式处理
                }
            })
            .api();
        //此处需调用api()方法,否则返回的是JQuery对象而不是DataTables的API对象
    });
</script>
</html>
